package com.systemdevelopment.mainproject.Model;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;


public class UserFactory {

    public static void main(String[] args) {        //测试代码

        boolean flag = insertUser("1001","password1001","NORMALUSER");//测试1  插入Normaluser到数据库
        if(flag){
            //插入成功 获取所有normaluser
            HashSet<NormalUser> normalUserHashSet = NormalUser.GetAllNormalUserFromDB();
            System.out.println(normalUserHashSet);//不知道能不能显示出来

        }else{
            //插入失败
        }


    }




    //向数据库的account_manager表中插入一行
    public static void InsertAccountIntoDB(String Account, String Authority, String Login_state){
        try{
            Connection conn = DataBaseManager.build_conn();
            Statement stmt = conn.createStatement();

            String Sql = "Insert into LIB.ACCOUNT_MANAGER VALUES("+ "'"+ Account + "'" + "," + "'" + Authority + "'" + "," + "'" + Login_state + "'" + ")";
            stmt.addBatch(Sql);
            stmt.executeBatch();
        }catch (SQLException Se){
            Se.printStackTrace();
        }
    }




    //插入new_User到数据库，如果是读者则插入READER表，如果是管理员则插入ADMINISTRATOR表
    //返回值true代表插入成功 false插入失败
    public static boolean insertUser(String account,String password,String authority) {
        try {
            Connection conn = DataBaseManager.build_conn();
            String Sql = "select * from LIB.ACCOUNT_MANAGER where ACCOUNT = " + "'" + account + "'";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);

            if(!rs.next()){
                //没有此账户 可以加入

                //插入account_manager
                InsertAccountIntoDB(account,authority,"ONLINE");//登陆状态ONLINE对应登陆  OFFLLINE对应下线

                if(authority.equals("NORMALUSER")){
                    //normaluser 插入READER
                    NormalUser normalUser = new NormalUser();
                    normalUser.setAccount(account);
                    normalUser.setPassword(password);
                    HashSet<NormalUser> NormalUserSet = new HashSet<>();
                    NormalUserSet.add(normalUser);
                    NormalUser.InsertNormalUserIntoDB(NormalUserSet);
                    return true;
                }else if(authority.equals(Admin.Authority) || authority.equals(SuperAdmin.Authority)){
                    //admin superadmin 插入ADMINESTRATOR
                    Admin admin = new Admin();
                    admin.setAccount(account);
                    admin.setPassword(password);
                    HashSet<Admin> AdminSet = new HashSet<>();
                    AdminSet.add(admin);
                    Admin.InsertAdminIntoDB(AdminSet);
                    return true;
                }else{
                    //authority 属性不识别 异常处理
                    return false;
                }
            }else{
                //已经有此账户 报错
                return false;
            }


        }catch (SQLException Se) {
            Se.printStackTrace();
        }

        return false;
    }


    //登陆，获取user对象
    public static NormalUser getUser(String Account,String password) {
        try {
            Connection conn = DataBaseManager.build_conn();
            String Sql = "select * from LIB.ACCOUNT_MANAGER where ACCOUNT = " + "'" + Account + "'";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);
            rs.next();
            String authority = rs.getString("AUTHORITY");
            String login_state = rs.getString("LOGIN_STATE");

            if (authority.length() > 0 && login_state.length() > 0) {
                switch (authority) {
                    case NormalUser.Authority:
                        //normaluser 查询READER表
                        NormalUser normalUser = NormalUser.GetNormalUserFromDBByAccount(Account);
                        if (Objects.equals(normalUser.getPassword(), password)) {
                            //密码正确
                            return normalUser;
                        } else {
                            //密码错误 可给出错误提示
                            return null;
                        }

                    case Admin.Authority: {
                        //admin
                        Admin admin = Admin.GetAdminFromDBByAccount(Account);
                        if (Objects.equals(admin.getPassword(), password)) {
                            //密码正确
                            return admin;
                        } else {
                            //密码错误 可给出错误提示
                            return null;
                        }

                    }
                    case SuperAdmin.Authority: {
                        //superadmin
                        SuperAdmin admin = SuperAdmin.GetSuperAdminFromDBByAccount(Account);
                        if (Objects.equals(admin.getPassword(), password)) {
                            //密码正确
                            return admin;
                        } else {
                            //密码错误 可给出错误提示
                            return null;
                        }
                    }
                    default:
                        //authority 属性出错 异常处理
                        break;
                }
            } else{
                //没找到此账户，异常处理
            }
        }catch (SQLException Se) {
            Se.printStackTrace();
        }

        return null;

    }
    public static ArrayList<NormalUser> getAllUser() {
        try {
            ArrayList<NormalUser> result=new ArrayList<>();
            Connection conn = DataBaseManager.build_conn();
            String Sql = "select * from LIB.ACCOUNT_MANAGER";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(Sql);
            while (rs.next()) {
                String authority = rs.getString("AUTHORITY");
                String Account=rs.getString("Account");
                    switch (authority) {
                        case NormalUser.Authority:
                            //normaluser 查询READER表
                            NormalUser normalUser = NormalUser.GetNormalUserFromDBByAccount(Account);
                            result.add(normalUser);
                            break;
                        case Admin.Authority: {
                            //admin
                            Admin admin = Admin.GetAdminFromDBByAccount(Account);
                            result.add(admin);
                            break;

                        }
                        case SuperAdmin.Authority: {
                            //superadmin
                            SuperAdmin admin = SuperAdmin.GetSuperAdminFromDBByAccount(Account);
                            result.add(admin);
                            break;

                        }
                        default:
                            //authority 属性出错 异常处理
                            break;
                    }
            }
            return result;
            }catch(SQLException Se){
                Se.printStackTrace();
            }
        return null;

    }

    //更改account_manager表中authority属性
    public static void UpdateAuthority(String account,String new_authority){
        try {
            Connection conn = DataBaseManager.build_conn();
            PreparedStatement preSql;

            String sqlStr = "update LIB.ACCOUNT_MANAGER set AUTHORITY = ? where ACCOUNT = ?";
            preSql = conn.prepareStatement(sqlStr);
            preSql.setString(1, new_authority);
            preSql.setString(2, account);
            preSql.executeUpdate();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //normaluser转换为admin
    public static void ChangeNormalUserToAdmin(NormalUser normalUser){
        Admin admin = new Admin();
        admin.setAccount(normalUser.getAccount());
        admin.setPassword(normalUser.getPassword());

        //删除reader表中的normaluser
        try{
            NormalUser.DeleteNormalUserFromREADER(normalUser.getAccount());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //把admin加入administrator表
        HashSet<Admin> AdminSet = new HashSet<>();
        AdminSet.add(admin);
        Admin.InsertAdminIntoDB(AdminSet);

        //更改account_manager中authority属性
        UpdateAuthority(normalUser.getAccount(),"ADMIN");
    }


    //admin转换为normaluser
    public static void ChangeAdminToNormalUser(Admin admin){
        NormalUser normalUser = new NormalUser();
        normalUser.setAccount(admin.getAccount());
        normalUser.setPassword(admin.getPassword());

        //删除administrator表中的admin
        try{
            Admin.DeleteAdminFromADMINISTRATOR(admin.getAccount());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //把normaluser加入reader表
        NormalUser.InsertNormalUserIntoDB(normalUser);

        //更改account_manager中authority属性
        UpdateAuthority(normalUser.getAccount(),"NORMALUSER");
    }




}
